package javaer.starter.idea.window.component;

import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.model.Dependency;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 * @author yangzhen
 * @date 2023/7/4 17:42
 * @since 1.0.0
 */
public class DependencyTable {

    public static final String[] fields = {"groupId", "artifactId", "version"};

    private JScrollPane scrollPane;
    private JTable jTable;

    public DependencyTable(List<Dependency> dependencies) {
        Object[][] rows = null;
        if (CollectionUtils.isNotEmpty(dependencies)) {
            rows = new Object[dependencies.size()][3];
            for (int i = 0; i < dependencies.size(); i++) {
                Dependency dependency = dependencies.get(i);
                rows[i][0] = dependency.getGroupId();
                rows[i][1] = dependency.getArtifactId();
                rows[i][2] = dependency.getVersion();
            }
        }
        DefaultTableModel model = new DefaultTableModel(rows, fields);
        this.jTable = new JTable(model);
        scrollPane = new JScrollPane(this.jTable);
    }

    public JTable getTable() {
        return this.jTable;
    }

    public JScrollPane getScrollPane() {
        return this.scrollPane;
    }
}
